<?php
/**
 * Created by PhpStorm.
 * User: huqi
 * Date: 2019-12-29
 * Time: 02:52
 */

namespace WebUtil\Matrix\Layer;


use WebUtil\Matrix\Parser;
use WebUtil\Matrix\Transform;

class Str2ArrayLayer implements ILayer
{
    private $input;
    private $keys;

    public function __construct($input,$keys)
    {
        $this->input = $input;
        $this->keys = $keys;
    }

    public function transform($input = null){
        if ($input === null){
            $input = [];
        }
        $parse = Parser::instance($this->input);
        $rs = [];
        while (!$parse->isEnd()){
            $line = trim($parse->fetchUtil(PHP_EOL));
            if (!$line){
                continue;
            }

            $lineParse = Parser::instance($line);
            $tmp = [];
            foreach ($this->keys as $key){
                $tmp[$key] = $lineParse->fetchWord();
            }
            $rs[] = $tmp;
        }
        $input[] = $rs;
        return $input;
    }
}